package com.feicheng.common.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.service.*;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class Cache extends JSONObject {
//    private Integer count = null;
//
//    public void initCount() {
//        count = 1;
//    }
//    public synchronized Integer incrementId() {
//        return count++;
//    }

    @Autowired
    IAmdService astoreService;

    @Autowired
    IAmjService iAmjService;

    @Autowired
    IAsettingService iAsettingService;

    @Autowired
    ICnRegionInfoService cnRegionInfoService;

    @Autowired
    IDictService dictService;

    private void initLocation() {
        JSONObject location = new JSONObject();
        List<CnRegionInfo> cns = cnRegionInfoService.list();
        for (CnRegionInfo cn : cns) {
            location.put(cn.getCriname(), cn);
        }
        this.put("cns", location);
    }

    public void initStore() {
        JSONObject mdsName = new JSONObject();
        JSONObject mdsId = new JSONObject();
        List<Amd> astores = astoreService.list();
        astores.forEach(md -> {
            mdsName.put(md.getMdmc(), md);
            mdsId.put(md.getMdid(), md);
        });
        this.put("mdsname", mdsName);
        this.put("mdsId", mdsName);
    }

    public void initDict() {
        JSONObject result = new JSONObject();
        List<Dict> dicts = dictService.list();
        dicts.forEach(dict -> {
            if (!result.containsKey(dict.getType())) {
                result.put(dict.getType(), new JSONObject());
            }

            JSONObject typeObj = result.getJSONObject(dict.getType());
            typeObj.put(dict.getCode(), dict);
            typeObj.put(dict.getValue(), dict);
        });
        this.put("dict", result);
    }

    @PostConstruct
    public void init() {
        this.clear();
        Asetting asetting = iAsettingService.getById("1");
        this.put("Asetting", asetting);

        this.initLocation();
        this.initStore();
        this.initDict();
        this.initMenjin();
        this.initMjByMd();
    }

    public void initMenjin() {
        this.put("allmenjin", new JSONObject());
    }

    public JSONObject getAllmenjin() {
        return this.getJSONObject("allmenjin");
    }

    public JSONObject getMenjin(String sn) {
        JSONObject allmenjin = this.getJSONObject("allmenjin");
        if (allmenjin.containsKey(sn)) {
            return allmenjin.getJSONObject(sn);
        }
        return null;
    }

    public void putMenjin(JSONObject menjin) {
        JSONObject allmenjin = this.getJSONObject("allmenjin");
        allmenjin.put(menjin.getString("sn"), menjin);
    }

    public void removeMenjin(String sn) {
        JSONObject allmenjin = this.getJSONObject("allmenjin");
        if (allmenjin.containsKey(sn)) {
            allmenjin.remove(sn);
        }
    }

    public void initMjByMd() {
        JSONObject mdItem = new JSONObject();
        QueryWrapper<Amj> qw = new QueryWrapper<>();
        qw.eq("dflg", false);
        List<Amj> mjs = iAmjService.list(qw);

        if (null != mjs && mjs.size() > 0) {
            mjs.forEach(mj -> {
                if (!mdItem.containsKey(mj.getMdid())) {
                    JSONArray mdsns = new JSONArray();
                    JSONObject fjObj = new JSONObject();
                    JSONObject md = new JSONObject();
                    md.put("mdsns", mdsns);
                    md.put("fjObj", fjObj);
                    mdItem.put(mj.getMdid(), md);
                }
                JSONObject md = mdItem.getJSONObject(mj.getMdid());
                JSONObject fjObj = md.getJSONObject("fjObj");
                JSONArray mdsns = md.getJSONArray("mdsns");

                if (StringUtils.hasLength(mj.getFjid()) && !"null".equals(mj.getFjid())) {
                    if (!fjObj.containsKey(mj.getFjid())) {
                        fjObj.put(mj.getFjid(), new JSONArray());
                    }
                    JSONArray fjsns = fjObj.getJSONArray(mj.getFjid());
                    fjsns.add(mj.getSn());
                } else {
                    mdsns.add(mj.getSn());
                }
            });
        }

        this.put("mdwithmj", mdItem);
    }

    public JSONObject getMdWithmj() {
        return this.getJSONObject("mdwithmj");
    }



}
